package tt.dz.egobus.servicemanager.business.point;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.bean.BaseBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author Anson
 * @date 17-3-23
 * @name bus的坐标点位置
 * @since 1.0.2
 */
public class BusPoint extends CommonPoint implements BaseBean, Point {


    private static final long serialVersionUID = 8547448731526640799L;

    private static final Logger LOGGER = LoggerFactory.getLogger(BusPoint.class);


    /**
     * @param json BusPoint 的字符串
     * @author Anson
     * @date 17-3-23
     * @name 将json字符串转换为bean
     * @since 1.0.2
     */
    public static BusPoint toBean(String json) {
        return JSON.parseObject(json, BusPoint.class);
    }

    /**
     * @param jsons 集合对象
     * @author Anson
     * @date 17-3-23
     * @name 将集合对象的字符串转换成对象
     * @since 1.0.2
     */
    public static List<BusPoint> toListBean(Collection<String> jsons) {
        if (jsons == null || jsons.isEmpty()) {
            return new ArrayList<BusPoint>(0);
        }

        List<BusPoint> list = new ArrayList<BusPoint>(jsons.size());
        for (String json : jsons) {
            try {
                list.add(BusPoint.toBean(json));
            } catch (Exception e) {
                LOGGER.error("json反序列化为BusPoint存在异常", e);
            }
        }
        return list;
    }


    /**
     * @author Anson
     * @date 17-3-24
     * @name 获取当前时间的差
     * @since 1.0.2
     */
    public long betweenNowTimeStamp() {
        return System.currentTimeMillis() - this.lastetUpdateTime;
    }

    /**
     * @author Anson
     * @date 17-3-24
     * @name 更新时间戳
     * @since 1.0.2
     */
    public void updateTime() {
        this.lastetUpdateTime = System.currentTimeMillis();
    }


    private String driverId;

    private long lastetUpdateTime;

    /**
     * 业务服务号
     */
    private int[] businessCodes;

    public BusPoint() {
        this.updateTime();
    }

    public BusPoint(String driverId) {
        this.driverId = driverId;
        this.updateTime();
    }

    public BusPoint(String driverId, double x, double y) {
        this.driverId = driverId;
        this.x = x;
        this.y = y;
        this.updateTime();
    }

    public BusPoint(String driverId, double x, double y, int[] businessCodes) {
        this.driverId = driverId;
        this.x = x;
        this.y = y;
        this.businessCodes = businessCodes;
        this.updateTime();
    }

    public String getDriverId() {
        return driverId;
    }

    public void setDriverId(String driverId) {
        this.driverId = driverId;
        this.updateTime();
    }

    @Override
    public void setX(double x) {
        super.setX(x);
        this.updateTime();
    }

    @Override
    public void setY(double y) {
        super.setY(y);
        this.updateTime();
    }

    public long getLastetUpdateTime() {
        return lastetUpdateTime;
    }

    public void setLastetUpdateTime(long lastetUpdateTime) {
        this.lastetUpdateTime = lastetUpdateTime;
    }

    public int[] getBusinessCodes() {
        return businessCodes;
    }

    public void setBusinessCodes(int[] businessCodes) {
        this.businessCodes = businessCodes;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BusPoint)) return false;

        BusPoint busPoint = (BusPoint) o;

        if (Double.compare(busPoint.getX(), getX()) != 0) return false;
        if (Double.compare(busPoint.getY(), getY()) != 0) return false;
        if (getLastetUpdateTime() != busPoint.getLastetUpdateTime()) return false;
        if (getDriverId() != null ? !getDriverId().equals(busPoint.getDriverId()) : busPoint.getDriverId() != null)
            return false;
        return Arrays.equals(businessCodes, busPoint.businessCodes);

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = getDriverId() != null ? getDriverId().hashCode() : 0;
        temp = Double.doubleToLongBits(getX());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(getY());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (int) (getLastetUpdateTime() ^ (getLastetUpdateTime() >>> 32));
        result = 31 * result + Arrays.hashCode(businessCodes);
        return result;
    }

    @Override
    public String toString() {
        return "BusPoint{" +
                "driverId='" + driverId + '\'' +
                ", x=" + x +
                ", y=" + y +
                ", lastetUpdateTime=" + lastetUpdateTime +
                ", businessCodes=" + Arrays.toString(businessCodes) +
                '}';
    }
}